package com.levi.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author shentong
 * @since 2022/1/16 5:15 PM
 * Thencombin将两个异步任务结果进行合并，有返回值
 */
public class ThenCombineDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "hello", executorService);

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "world", executorService);

        CompletableFuture<Object> future3 = future1.thenCombineAsync(future2, (f1, f2) -> f1 + f2, executorService);
        try {
            System.out.println("执行结果是：>>"+future3.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }
}
